﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game 
{
	import away3d.cameras.*;
	import away3d.containers.*;
	import away3d.core.utils.*;
	import away3d.lights.*;
	import away3d.materials.*;
	import away3d.primitives.*;
	import away3d.loaders.Collada;
	import away3d.loaders.Object3DLoader;
	import com.sng.loader.SceneLoader;
	
	import flash.display.*;
	import flash.events.*;
	
	[SWF(frameRate="60", quality="LOW", width="390", height="512")]
	
	/**
	 * ...
	 * @author snega
	 */
	public class Game extends com.sng.stg.game.Stage
	{
		protected var _sceneLoader:SceneLoader = null;
		
		public function Game() 
		{
			super();
			
			this._2dMask.addChild(this._loading);
			
			this._sceneLoader = new SceneLoader("data/scene.xml");
			
			this.addEventListener(Event.ENTER_FRAME, onSceneLoadingEnterFrame);
		}
		
		public function onSceneLoadingEnterFrame(e:Event):void
		{
			this._loading.text = "loading ... (" + (this._sceneLoader._imageLoadedNum + this._sceneLoader._modelLoadedNum) + "/" + (this._sceneLoader._imageNum + this._sceneLoader._modelNum) + ")";
			this._loading.setTextFormat(this._loadingFormat);
			
			if (this._sceneLoader._load2DComplete && this._sceneLoader._load3DComplete) {
				// remove all objects
				var max:int = this._2dMask.numChildren;
				for (var i:int = 0; i < max; i++) {
					this._2dMask.removeChildAt(0);
				}
								
				// show title
				this.removeEventListener(Event.ENTER_FRAME, onSceneLoadingEnterFrame);
				
				this._2dMask.addChild(new Bitmap(this._sceneLoader._imageBitmapMap["title"]));
				
				stage.addEventListener(KeyboardEvent.KEY_UP, menuControl);
			}
		}
		
		public function startGame():void {
			// remove all objects
			var max:int = this._2dMask.numChildren;
			for (var i:int = 0; i < max; i++) {
				this._2dMask.removeChildAt(0);
			}
							
			// begin
			this.go("data/stage1.xml");
		}
		
		public function menuControl(event:KeyboardEvent):void {
			if (event.keyCode == StageViewer.KEY_Z) {
				stage.removeEventListener(KeyboardEvent.KEY_UP, menuControl);
				
				this._imageBitmapMap["loading"] = this._sceneLoader._imageBitmapMap["loading"];
				this._imageBitmapMap["game_over"] = this._sceneLoader._imageBitmapMap["game_over"];
				this._imageBitmapMap["stage_clear"] = this._sceneLoader._imageBitmapMap["stage_clear"];
				
				this.startGame();
				
				trace("game start!");
			}
		}
		
	}
	
}